package com.whc.gulimall.product.app;

import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.common.valid.AddGroup;
import com.whc.common.valid.UpdateGroup;
import com.whc.gulimall.product.entity.SpuInfoEntity;
import com.whc.gulimall.product.service.SpuInfoService;
import com.whc.gulimall.product.vo.ProductInfoVo;
import com.whc.gulimall.product.vo.SpuSaveVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * spu信息
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:18:53
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {

    /**
     * spu信息
     */
    @Resource
    private SpuInfoService spuInfoService;

    /**
     * 列表
     *
     * @param params 查询条件及分页数据
     * @return 统一返回结果
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuInfoService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@Validated({AddGroup.class, UpdateGroup.class}) @RequestBody SpuSaveVo spuSaveVo) {
        spuInfoService.saveSpu(spuSaveVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 上架商品
     *
     * @param spuId spuId
     * @return 统一返回结果
     */
    @PostMapping("/{spuId}/up")
    public R upSpu(@PathVariable("spuId") Long spuId) {
        spuInfoService.upSpu(spuId);
        return R.ok();
    }

    /**
     * 通过skuId列表获取商品信息列表
     *
     * @param skuIds skuId列表
     * @return 统一返回结果 Map<Long, ProductInfoVo>
     */
    @PostMapping("/product/infos")
    public R listProductInfos(@RequestBody List<Long> skuIds) {
        Map<Long, ProductInfoVo> infoVoMap = spuInfoService.listProductInfos(skuIds);
        return R.ok().setData(infoVoMap);
    }

}
